package controllers

import play.api.i18n.I18nSupport
import play.api.mvc.{AbstractController, ControllerComponents}

import javax.inject.Inject
import upickle.default._
import implicits.Implicits._
import tool.{FormTool, Tool}

import java.io.File
import scala.concurrent.ExecutionContext.Implicits.global

/**
 * Created by yz on 6/1/2021
 */
class ToolController @Inject()(cc: ControllerComponents) extends AbstractController(cc) with I18nSupport{

  def getAllMessages = Action { implicit request =>
    val kind = request.lang.code
    val messages = messagesApi.messages(kind)
    Ok(writeJs(messages))
  }

  def downloadExampleData = Action {
    implicit request =>
      val data = FormTool.fileNameForm.bindFromRequest().get
      val exampleDir = Tool.exampleDir
      val resultFile = new File(exampleDir, data.fileName)
      Ok.sendFile(resultFile).withHeaders(
        CONTENT_DISPOSITION -> s"attachment; filename=${
          resultFile.getName
        }",
        CONTENT_TYPE -> "application/x-download"
      )
  }

}
